package com.example.ql.express.controller;

import com.example.ql.express.mapper.ScriptConfigMapper;
import com.example.ql.express.model.dto.BabyConfig;
import com.example.ql.express.model.entity.ScriptConfigDO;
import com.example.ql.express.model.vo.ScriptConfigVO;
import com.example.ql.express.utils.MessageConvertUtils;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xuweizhi
 * @since 2021/10/29 01:06
 */
@RestController
@Api(tags = "脚本转换")
@RequestMapping("scriptConfig")
public class ScriptConfigController {

    @Resource
    private ExpressRunner expressRunner;

    @Resource
    private ScriptConfigMapper scriptConfigMapper;


    @PostMapping("save")
    @ApiOperation("save")
    public void save(@RequestBody ScriptConfigVO scriptConfigVO) {
        // com.example.ql.express.config.ScriptConfig scriptConfig = new com.example.ql.express.config.ScriptConfig();
        // scriptConfigVO.setFiledType(ClassEnum.INT);
        // scriptConfigVO.setFiledName(Arrays.asList("breathe", "systolicPressure"));
        // scriptConfigVO.setOperate(OperateEnum.COMBINATION);
        // String compareRule = "source.vitalSign.breathe > 20 && source.vitalSign.systolicPressure >= 220 && " +
        //         "(source.disease.patientType == 2 || source.disease.patientType == 4)";
        // scriptConfigVO.setValidateRule(Arrays.asList("source.vitalSign.breathe != null", "source.vitalSign.systolicPressure != null"));
        // scriptConfigVO.setCompareRule(compareRule);
        // scriptConfigVO.setLevels(Collections.singletonList("1"));
        // scriptConfigVO.setParentPath(FieldConstant.COMBINATION);
        // scriptConfigVO.setAppendRule(AppendRuleEnum.DEFAULT);
        ScriptConfigDO scriptConfigDO = new ScriptConfigDO();
        BeanUtils.copyProperties(scriptConfigVO, scriptConfigDO);
        scriptConfigMapper.insert(scriptConfigDO);
    }

    @GetMapping("get")
    @ApiOperation("get")
    public ScriptConfigDO get(Long id) {
        ScriptConfigDO scriptConfig = scriptConfigMapper.selectById(id);
        return scriptConfig;
    }

    @GetMapping("scriptConvert")
    @ApiOperation("scriptConvert")
    public String scriptConvert() throws Exception {

        List<ScriptConfigDO> scriptConfigList = scriptConfigMapper.selectList(null);
        String expressScript = MessageConvertUtils.messageConvert(scriptConfigList.stream().map(ScriptConfigDO::of).collect(Collectors.toList()));

        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        context.put("source", BabyConfig.buildBabyConfig());
        Object r = expressRunner.execute(expressScript, context, null, true, false);
        System.out.println(expressScript);
        System.out.println("匹配结果:" + r.toString());
        return expressScript;
    }
}
